import createPersistedState from 'vuex-persistedstate'
import Cookies from 'js-cookie'
const cookieOp = {
  getItem: (key) => Cookies.get(key),
  setItem: (key, value) =>
    Cookies.set(key, value, { expires: 3, secure: true }),
  removeItem: (key) => Cookies.remove(key)
}
const sessionStorageOp = {
  getItem: (key) => sessionStorage.getItem(key),
  setItem: (key, value) =>
    sessionStorage.setItem(key, value),
  removeItem: (key) => sessionStorage.removeItem(key)
}
const localStorageOp = {
  getItem: (key) => localStorage.getItem(key),
  setItem: (key, value) =>
    localStorage.setItem(key, value),
  removeItem: (key) => localStorage.removeItem(key)
}

export default (config) => {
  let storageOP = localStorageOp
  const { position } = config
  if (position === 'cookie') {
    storageOP = cookieOp
  } else if (position === 'sessionCookie') {
    storageOP = sessionStorageOp
  }
  return createPersistedState({
    ...config,
    storage: storageOP
  })
}
